var express = require('express');
const bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json())
const Queue = require('./taskqueue')
const mergeTaskQueue  = new Queue()
const getCode = require('./init')
const config = require('./config')
// gitlab触发merge请求
app.post('/update', function (req, res, next) {
  let git_ssh_url = req.body.project.git_ssh_url;
  let name = req.body.project.name;
  // 上线merge分支master
  if (req.headers['x-gitlab-token'] == 'pushDev') {
      if (config[name] && config[name].git_ssh_url == git_ssh_url) {
          mergeTaskQueue.addTask(function () {
              getCode.init(git_ssh_url, name, '1.0.0').then(function (data) {
                  console.log(data);
                  mergeTaskQueue.run();
              }).catch(function (error) {
                  console.log(error);
                  mergeTaskQueue.run();
              })
          }.bind(null, git_ssh_url, name));
      }
      res.end('receive request');
      // 测试merge分支dev
  }  else {
      return res.end('receive request');
  }
})

var server = app.listen(8082, function () {

  var host = server.address().address
  var port = server.address().port

  console.log("应用实例，访问地址为 http://%s:%s", host, port)

})

